package com.ddc.study.feature.main.home;

import com.ddc.study.entity.Article;
import com.ddc.study.entity.Banner;
import com.ddc.study.entity.MainData;
import com.ddc.study.net.NetworkUtil;
import com.ddc.study.net.response.BaseResponse;

import java.util.List;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created on 2018/3/30.
 *
 * @author lshun
 * @version 1.0
 */
class HomePresenter extends HomeContract.IPresenter {
    @Override
    void loadBanner() {
        Runnable runnable = () -> {
            final List<Banner> data = NetworkUtil.getBanner();

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showBanner(data);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void loadData(@IntRange(from = 0) final int page) {
        if (page < 0) return;

        Runnable runnable = () -> {
            final MainData<Article> data = NetworkUtil.getMainArticleList(page);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showData(data);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void collectArticle(final int id) {
        //未登录则需要先登录再能操作
        if (!getView().isLoggedIn()) {
            getView().needLogin();
            return;
        }

        NetworkUtil.collectInnerArticle(id).enqueue(new Callback<BaseResponse<Object>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<Object>> call,
                                   @NonNull Response<BaseResponse<Object>> response) {
                if (!isViewAttached()) return;
                if (!response.isSuccessful()) return;
                BaseResponse<Object> body = response.body();
                if (body == null) return;
                if (body.isSuccess()) {
                    getView().showCollectResult(id, true);
                } else {
                    if (body.isIdentityExpiry()) {
                        getView().needLogin();
                    }
                    getView().showCollectResult(id, false);
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<Object>> call,
                                  @NonNull Throwable t) {
                if (!isViewAttached()) return;
                getView().showCollectResult(id, false);
            }
        });
    }

    @Override
    void unCollectArticle(final int id) {
        NetworkUtil.unCollectArticle1(id).enqueue(new Callback<BaseResponse<Object>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<Object>> call,
                                   @NonNull Response<BaseResponse<Object>> response) {
                if (!isViewAttached()) return;
                if (!response.isSuccessful()) {
                    getView().showUnCollectResult(id, false);
                    return;
                }
                BaseResponse<Object> body = response.body();
                if (body == null) {
                    getView().showUnCollectResult(id, false);
                } else if (!body.isSuccess()) {
                    getView().showUnCollectResult(id, false);
                } else {
                    getView().showUnCollectResult(id, true);
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<Object>> call,
                                  @NonNull Throwable t) {
                if (isViewAttached()) getView().showUnCollectResult(id, false);
            }
        });
    }
}
